home *** CD-ROM | disk | FTP | other *** search
/ Kodak Picture CD: 357071 / Kodak Picture CD 357071.iso / setup / ksu / ksu.cab / pacsupport.js < prev    next >
Text File  |  2004-02-13  |  12KB  |  463 lines

  1. // Dummy 'alert' function (supported by IE):
  2. function alert() {}
  3.  
  4. // Note that 'helper' will be defined before including this code
  5.  
  6. // Resolve a host name:
  7. function dnsResolve(host) { return helper.dnsResolve(host); }
  8.  
  9. // Get local IP address:
  10. function myIpAddress() { return helper.myIpAddress(); }
  11.  
  12. // Check if a host name can be resolved:
  13. function isResolvable(host) { return helper.isResolvable(host); }
  14.  
  15. // Cehck if the IP address of the host matches the spcified IP address pattern:
  16. function isInNet(host, pattern, mask) { return helper.isInNet(host, pattern, mask); }
  17.  
  18. // Check if a given host name is plain (no dots):
  19. function isPlainHostName(host)
  20. {
  21.     return (host.search(/\./) == -1);
  22. }
  23.  
  24. // Check if the given host name belongs to the given domain:
  25. function dnsDomainIs(host, domain)
  26. {
  27.     if (domain.length > host.length)
  28.         return false;
  29.     return (host.substr(host.length - domain.length) == domain);
  30. }
  31.  
  32. // Check if given host name matches the fully qualified host name (host + domain):
  33. function localHostOrDomainIs(host, hostdom)
  34. {
  35.     if (host == hostdom)
  36.         return true;
  37.  
  38.     var spHostdom = hostdom.split(".");
  39.     if (host == spHostdom[0])
  40.         return true;
  41.  
  42.     return false;
  43. }
  44.  
  45. // Number of DNS domains level in a host name (number of dots):
  46. function dnsDomainLevels(host)
  47. {
  48.     var ret = 0;
  49.     for (i = 0; i < host.length; i++)
  50.         if (host.charAt(i) == '.')
  51.             ret++;
  52.     return ret;
  53. }
  54.  
  55. // Match a shell expression:
  56. // - Slash and backslash are interchangeable
  57. // - Match is case insensitive
  58. // - '*' is interpreted and "any sequence of characters"
  59. function shExpMatch(str, shexp)
  60. {
  61.     // Build a regular expression:
  62.     var star = /\*/g;
  63.     var slash = /\/|\\/g;
  64.     var dot = /\./g;
  65.     var regexpstr = shexp;
  66.     regexpstr = regexpstr.replace(slash, "[/\\\\]");
  67.     regexpstr = regexpstr.replace(dot, "\\.");
  68.     regexpstr = regexpstr.replace(star, ".*");
  69.  
  70.     var regexp = new RegExp(regexpstr, "i");    // case insensitive
  71.     var result = str.match(regexp);
  72.     if (result == null)
  73.         return false;
  74.     return (result[0] == str);        // true only if regexp matchs the complete string
  75. }
  76.  
  77. // Auxiliary function to find the index of a given weekday (0-6):
  78. function findWeekdayIndex(wd)
  79. {
  80.     var weekdays = new Array("SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT");
  81.     for (i = 0; i < 7; i++) {
  82.         if (wd == weekdays[i])
  83.             return i;
  84.     }
  85.     return -1;
  86. }
  87.  
  88. // Check if the current weekday is within the given range.
  89. function weekdayRange()
  90. {
  91.     var now = new Date();
  92.     var nargs = arguments.length;
  93.  
  94.     if (nargs == 1) {
  95.         var index = findWeekdayIndex(arguments[0]);
  96.         if (index == -1)
  97.             return false;    // given weekday is invalid
  98.  
  99.         return (now.getDay() == index);
  100.     }
  101.     else if (nargs == 2) {
  102.         if (arguments[1] == "gmt") {
  103.             var index = findWeekdayIndex(arguments[0]);
  104.             if (index == -1)
  105.                 return false;
  106.  
  107.             return (now.getUTCDay() == index);
  108.         }
  109.         else {
  110.             var wd1 = findWeekdayIndex(arguments[0]);
  111.             var wd2 = findWeekdayIndex(arguments[1]);
  112.             if (wd1 == -1 || wd2 == -1)
  113.                 return false;
  114.  
  115.             if (wd2 >= wd1)        // e.g. SUN-WED
  116.                 return (now.getDay() >= wd1 && now.getDay() <= wd2);
  117.             else                // e.g. WED-SUN
  118.                 return (now.getDay() >= wd1 || now.getDay() <= wd2);
  119.         }
  120.     }
  121.     else if (nargs == 3) {
  122.         if (arguments[2] != "gmt")
  123.             return false;
  124.  
  125.         var wd1 = findWeekdayIndex(arguments[0]);
  126.         var wd2 = findWeekdayIndex(arguments[1]);
  127.         if (wd1 == -1 || wd2 == -1)
  128.             return false;
  129.  
  130.         if (wd2 >= wd1)        // e.g. SUN-WED
  131.             return (now.getUTCDay() >= wd1 && now.getUTCDay() <= wd2);
  132.         else                // e.g. WED-SUN
  133.             return (now.getUTCDay() >= wd1 || now.getUTCDay() <= wd2);
  134.     }
  135.     else
  136.         return false;
  137. }
  138.  
  139. // Auxiliary function to find the index of a given month (0-11):
  140. function findMonthIndex(month)
  141. {
  142.     var months = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN",
  143.                             "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
  144.     for (i = 0; i < 12; i++) {
  145.         if (month == months[i])
  146.             return i;
  147.     }
  148.     return -1;
  149. }
  150.  
  151. // Check if the current date is within the given range.
  152. function dateRange()
  153. {
  154.     var now = new Date();
  155.     var nargs = arguments.length;
  156.     
  157.     if (typeof(arguments[0]) == "string") {
  158.         // First argument is a month
  159.         if (nargs == 1) {
  160.             var index = findMonthIndex(arguments[0]);
  161.             if (index == -1)
  162.                 return false;    // given month is invalid
  163.             else
  164.                 return (now.getMonth() == index);
  165.         }
  166.         else if (nargs == 2) {
  167.             if (arguments[1] == "gmt") {
  168.                 var index = findMonthIndex(arguments[0]);
  169.                 if (index == -1)
  170.                     return false;
  171.                 else
  172.                     return (now.getUTCMonth() == index);
  173.             }
  174.             else {
  175.                 var from = findMonthIndex(arguments[0]);
  176.                 var to = findMonthIndex(arguments[1]);
  177.                 if (from == -1 || to == -1)
  178.                     return false;
  179.                 return (now.getMonth() >= from && now.getMonth() <= to);
  180.             }
  181.         }
  182.         else if (nargs == 3) {
  183.             if (arguments[2] == "gmt") {
  184.                 var from = findMonthIndex(arguments[0]);
  185.                 var to = findMonthIndex(arguments[1]);
  186.                 if (from == -1 || to == -1)
  187.                     return false;
  188.                 return (now.getUTCMonth() >= from && now.getMonth() <= to);
  189.             }
  190.             else
  191.                 return false;
  192.         }
  193.         else if (nargs == 4) {
  194.             var m1 = findMonthIndex(arguments[0]);
  195.             var y1 = arguments[1];
  196.             var m2 = findMonthIndex(arguments[2]);
  197.             var y2 = arguments[3];
  198.             
  199.             if (m1 == -1 || m2 == -1)
  200.                 return false;
  201.  
  202.             if ((now.getFullYear() < y1) ||
  203.                 (now.getFullYear() == y1 && now.getMonth() < m1) )
  204.                 return false;    // not yet;
  205.  
  206.             if ((now.getFullYear() > y2) ||
  207.                 (now.getFullYear() == y2 && now.getMonth() > m2) )
  208.                 return false;    // already past
  209.  
  210.             return true;
  211.         }
  212.         else if (nargs == 5) {
  213.             if (arguments[4] != "gmt")
  214.                 return false;
  215.  
  216.             var m1 = findMonthIndex(arguments[0]);
  217.             var y1 = arguments[1];
  218.             var m2 = findMonthIndex(arguments[2]);
  219.             var y2 = arguments[3];
  220.             
  221.             if (m1 == -1 || m2 == -1)
  222.                 return false;
  223.  
  224.             if ((now.getUTCFullYear() < y1) ||
  225.                 (now.getUTCFullYear() == y1 && now.getUTCMonth() < m1) )
  226.                 return false;    // not yet;
  227.  
  228.             if ((now.getUTCFullYear() > y2) ||
  229.                 (now.getUTCFullYear() == y2 && now.getUTCMonth() > m2) )
  230.                 return false;    // already past
  231.  
  232.             return true;
  233.         }
  234.         else    // invalid number of arguments
  235.             return false;
  236.     }
  237.  
  238.     if (typeof(arguments[0]) != "number")
  239.         return false;
  240.  
  241.     if (arguments[0] <= 31) {
  242.         // First argument is a day
  243.         if (nargs == 1) {
  244.             return (now.getDate() == arguments[0]);
  245.         }
  246.         else if (nargs == 2) {
  247.             if (arguments[1] == "gmt")
  248.                 return (now.getUTCDate() == arguments[0]);
  249.             else
  250.                 return (now.getDate() >= arguments[0] && now.getDate() <= arguments[1]);
  251.         }
  252.         else if (nargs == 3) {
  253.             if (arguments[2] == "gmt")
  254.                 return (now.getUTCDate() >= arguments[0] && now.getUTCDate() <= arguments[1]);
  255.             else
  256.                 return false;
  257.         }
  258.         else if (nargs == 4) {
  259.             var d1 = arguments[0];
  260.             var m1 = findMonthIndex(arguments[1]);
  261.             var d2 = arguments[2];
  262.             var m2 = findMonthIndex(arguments[3]);
  263.  
  264.             if (m1 == -1 || m2 == -1)
  265.                 return false;
  266.  
  267.             if ((now.getMonth() < m1) ||
  268.                 (now.getMonth() == m1 && now.getDate() < d1) )
  269.                 return false;    // not yet;
  270.  
  271.             if ((now.getMonth() > m2) ||
  272.                 (now.getMonth() == m2 && now.getDate() > d2) )
  273.                 return false;    // already past
  274.  
  275.             return true;
  276.         }
  277.         else if (nargs == 5) {
  278.             if (arguments[4] != "gmt")
  279.                 return false;
  280.  
  281.             var d1 = arguments[0];
  282.             var m1 = findMonthIndex(arguments[1]);
  283.             var d2 = arguments[2];
  284.             var m2 = findMonthIndex(arguments[3]);
  285.  
  286.             if (m1 == -1 || m2 == -1)
  287.                 return false;
  288.  
  289.             if ((now.getUTCMonth() < m1) ||
  290.                 (now.getUTCMonth() == m1 && now.getUTCDate() < d1) )
  291.                 return false;    // not yet;
  292.  
  293.             if ((now.getUTCMonth() > m2) ||
  294.                 (now.getUTCMonth() == m2 && now.getUTCDate() > d2) )
  295.                 return false;    // already past
  296.  
  297.             return true;
  298.         }
  299.         else if (nargs == 6) {
  300.             var d1 = arguments[0];
  301.             var m1 = findMonthIndex(arguments[1]);
  302.             var y1 = arguments[2]
  303.             var d2 = arguments[3];
  304.             var m2 = findMonthIndex(arguments[4]);
  305.             var y2 = arguments[5];
  306.  
  307.             if (m1 == -1 || m2 == -1)
  308.                 return false;
  309.  
  310.             if ((now.getFullYear() < y1) ||
  311.                 (now.getFullYear() == y1 && now.getMonth() < m1) ||
  312.                 (now.getFullYear() == y1 && now.getMonth() == m1 && now.getDate() < d1) )
  313.                 return false;    // not yet;
  314.  
  315.             if ((now.getFullYear() > y2) ||
  316.                 (now.getFullYear() == y2 && now.getMonth() > m2) ||
  317.                 (now.getFullYear() == y2 && now.getMonth() == m2 && now.getDate() > d2) )
  318.                 return false;    // already past
  319.  
  320.             return true;
  321.         }
  322.         else if (nargs == 7) {
  323.             if (arguments[6] != "gmt")
  324.                 return false;
  325.  
  326.             var d1 = arguments[0];
  327.             var m1 = findMonthIndex(arguments[1]);
  328.             var y1 = arguments[2]
  329.             var d2 = arguments[3];
  330.             var m2 = findMonthIndex(arguments[4]);
  331.             var y2 = arguments[5];
  332.  
  333.             if (m1 == -1 || m2 == -1)
  334.                 return false;
  335.  
  336.             if ((now.getUTCFullYear() < y1) ||
  337.                 (now.getUTCFullYear() == y1 && now.getUTCMonth() < m1) ||
  338.                 (now.getUTCFullYear() == y1 && now.getUTCMonth() == m1 && now.getUTCDate() < d1) )
  339.                 return false;    // not yet;
  340.  
  341.             if ((now.getUTCFullYear() > y2) ||
  342.                 (now.getUTCFullYear() == y2 && now.getUTCMonth() > m2) ||
  343.                 (now.getUTCFullYear() == y2 && now.getUTCMonth() == m2 && now.getUTCDate() > d2) )
  344.                 return false;    // already past
  345.  
  346.             return true;
  347.         }
  348.         else
  349.             return false;
  350.     }
  351.     else {
  352.         // First argument is a year
  353.         if (nargs == 1) {
  354.             return (now.getFullYear() == arguments[0]);
  355.         }
  356.         else if (nargs == 2) {
  357.             return (now.getFullYear() >= arguments[0] && now.getFullYear() <= arguments[1]);
  358.         }
  359.         else if (nargs == 3) {
  360.             if (arguments[2] != "gmt")
  361.                 return false;
  362.             return (now.getUTCFullYear() >= arguments[0] && now.getUTCFullYear() <= arguments[1]);
  363.         }
  364.         else
  365.             return false;
  366.     }
  367. }
  368.  
  369. // Check if the current time is within the given time range.
  370. function timeRange()
  371. {
  372.     var now = new Date();
  373.     var h1, h2, min1 = 0, min2 = 59, sec1 = 0, sec2 = 59;
  374.     var gmt = 0;
  375.  
  376.     switch (arguments.length)
  377.     {
  378.         case 1:
  379.             h1 = arguments[0];
  380.             h2 = h1 + 1;
  381.             break;
  382.         case 2:
  383.             if (typeof(arguments[1]) == "number") {
  384.                 h1 = arguments[0];
  385.                 h2 = arguments[1];
  386.             }
  387.             else {
  388.                 h1 = arguments[0];
  389.                 h2 = h1 + 1;
  390.                 if (arguments[1] == "gmt")
  391.                     gmt = 1;
  392.             }
  393.             break;
  394.         case 3:
  395.             h1 = arguments[0];
  396.             h2 = arguments[1];
  397.             if (arguments[2] == "gmt")
  398.                 gmt = 1;
  399.             break;
  400.         case 4:
  401.             h1 = arguments[0];
  402.             min1 = arguments[1];
  403.             h2 = arguments[2];
  404.             min2 = arguments[3];
  405.             break;
  406.         case 5:
  407.             h1 = arguments[0];
  408.             min1 = arguments[1];
  409.             h2 = arguments[2];
  410.             min2 = arguments[3];
  411.             if (arguments[4] == "gmt")
  412.                 gmt = 1;
  413.             break;
  414.         case 6:
  415.             h1 = arguments[0];
  416.             min1 = arguments[1];
  417.             sec1 = arguments[2];
  418.             h2 = arguments[3];
  419.             min2 = arguments[4];
  420.             sec2 = arguments[5];
  421.             break;
  422.         case 7:
  423.             h1 = arguments[0];
  424.             min1 = arguments[1];
  425.             sec1 = arguments[2];
  426.             h2 = arguments[3];
  427.             min2 = arguments[4];
  428.             sec2 = arguments[5];
  429.             if (arguments[6] == "gmt")
  430.                 gmt = 1;
  431.             break;
  432.         default:
  433.             return false;    // invalid parameters
  434.     }
  435.  
  436.     if (gmt) {
  437.         if ((now.getUTCHours() < h1) ||
  438.             (now.getUTCHours() == h1 && now.getUTCMinutes() < min1) ||
  439.             (now.getUTCHours() == h1 && now.getUTCMinutes() == min1 && now.getUTCSeconds() < sec1) )
  440.             return false;        // not yet
  441.  
  442.         if ((now.getUTCHours() > h2) ||
  443.             (now.getUTCHours() == h2 && now.getUTCMinutes() > min2) ||
  444.             (now.getUTCHours() == h2 && now.getUTCMinutes() == min2 && now.getUTCSeconds() > sec2) )
  445.             return false;        // already past
  446.  
  447.         return true;
  448.     }
  449.     else {
  450.         if ((now.getHours() < h1) ||
  451.             (now.getHours() == h1 && now.getMinutes() < min1) ||
  452.             (now.getHours() == h1 && now.getMinutes() == min1 && now.getSeconds() < sec1) )
  453.             return false;        // not yet
  454.  
  455.         if ((now.getHours() > h2) ||
  456.             (now.getHours() == h2 && now.getMinutes() > min2) ||
  457.             (now.getHours() == h2 && now.getMinutes() == min2 && now.getSeconds() > sec2) )
  458.             return false;        // already past
  459.  
  460.         return true;
  461.     }
  462. }
  463.